/*
 * TransmissionMonitor.fx
 *
 * Created on 2010-05-29, 16:42:45
 */

package ssrw;

import javafx.scene.CustomNode;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.part.NumberAxis;
import javafx.scene.chart.part.Side;
import javafx.scene.text.Font;
import javafx.scene.Node;
import javafx.util.Math;

/**
 * @author azaleski
 */
 public class TransmisionMonitor extends CustomNode {
     
     override protected function create () : Node {
         transmisionLineChart;
     }


     public-init var width = 300;
     public-init var height = 280;
     public var x : Number;
     public var y : Number;

     var transmisionLineChartSeries : LineChart.Series[] = [
         LineChart.Series { name: "Moc transmisji terminala mobilnego [Mobile->BTS]" }
     ];

     var transmisionLineChart = LineChart {
         translateX: 10
         translateY: 10
         showSymbols: false
         legendSide: Side.TOP
         data: transmisionLineChartSeries

         xAxis: NumberAxis {
             lowerBound: 1
             upperBound: 100
             tickUnit: 10
             label: "Miliseconds"
             labelFont: Font { size: 10 }
             tickLabelTickGap: 10
             tickLabelFont: Font { size: 9 }
             labelTickGap: 10
             tickMarkLength: 10
         }
         yAxis: NumberAxis {
             lowerBound: -120
             upperBound: 20
             tickUnit: 20
             label: "[dBm]"
             labelFont: Font { size: 10 }
             tickLabelTickGap: 10
             tickLabelFont: Font { size: 9 }
             labelTickGap: 10
             tickMarkLength: 10
             tickMarkVisible: false
         }
         height: height
         width: width
         layoutX : x
         layoutY: y
     }

     var transmisionIndex = 0;
     public var dbm_BTS_TransmisionReceivedPowerValue = 0.0;

     var rayleighRandom = 0.0;


     public function updateTransmision(dbm_MS_ReceivedPilotPowerValue: Double, db_longDistancePathLoss: Double) {
         
         rayleighRandom = 20*Math.log10(Math.sqrt((-Math.log(1-(Math.random())))));

         dbm_BTS_TransmisionReceivedPowerValue = -dbm_MS_ReceivedPilotPowerValue - 73 + 2 + 0 + 0 + rayleighRandom - db_longDistancePathLoss;

         insert LineChart.Data {
             xValue: transmisionIndex
             yValue: dbm_BTS_TransmisionReceivedPowerValue
         } into transmisionLineChartSeries[0].data;

         transmisionIndex += 1;
         if(transmisionIndex >= transmisionLineChart.xAxis.upperBound) {
             /**
              * Shift line chart
              */
             transmisionLineChart.xAxis.lowerBound += 1;
             transmisionLineChart.xAxis.upperBound += 1;

             // Delete initial data
             delete transmisionLineChartSeries[0].data[0];
         }
     }
 }